package com.scrcu.cloud.wechat.home.endpoint.customer.utils;


import com.scrcu.cloud.wechat.home.endpoint.customer.vo.DayInfo;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.GregorianCalendar;

/**
 * @auther: siliang liu
 * @date: 2024/2/29 15:20
 * @description: 日期处理工具类
 * @version: 1.0.0
 */
public class DateUtil {

    /**
     * 获取指定格式的服务器事件
     *
     * @param formatter 时间格式
     * @return string
     */
    public static String getServeTime(String formatter) {
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern(formatter);
        return LocalDateTime.now().format(pattern);
    }

    /**
     * 获取无分隔符格式服务器时间
     * yyyyMMddHHmmss
     *
     * @return string
     */
    public static String getServeTime() {
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern("yyyyMMddHHmmss");
        return LocalDateTime.now().format(pattern);
    }

    /**
     * 默认时间格式
     *
     * @return string
     */
    public static String localTime() {
        return getServeTime("yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 格式化时间
     *
     * @param dateTime 时间
     * @param format   格式
     * @return string
     */
    public static String formatTime(String dateTime, String format) {
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern(format);
        LocalDateTime parse = LocalDateTime.parse(dateTime, pattern);
        return parse.format(pattern);
    }

    /**
     * 格式化时间
     *
     * @param dateTime 时间
     * @param format   格式
     * @return string
     */
    public static String formatTime(LocalDateTime dateTime, String format) {
        DateTimeFormatter pattern = DateTimeFormatter.ofPattern(format);
        return dateTime.format(pattern);
    }

    /**
     * 默认时间格式
     *
     * @return string
     */
    public static String nonSignTime() {
        return getServeTime("yyyyMMddHHmmss");
    }

    /**
     * 获取当天剩余秒数
     *
     * @return long
     */
    public static long remainSecOfDay() {
        LocalDateTime now = LocalDateTime.now();
        LocalDateTime midnight = now.plusDays(1).toLocalDate().atStartOfDay();
        return ChronoUnit.SECONDS.between(now, midnight);
    }

    /**
     * 获取当前指定时间 格式yyyyMMddHHmmss
     * @param dateOffset 日期偏移量-天（正负值）
     * @param timeOffset 时间偏移量-秒（正负值）
     * @return
     */
    public static String getDifferentTime(int dateOffset,int timeOffset){
        return getDifferentTime(dateOffset,timeOffset,"yyyyMMddHHmmss");
    }

    /**
     * 获取当前指定时间、指定格式
     * @param dateOffset 日期偏移量-天（正负值）
     * @param timeOffset 时间偏移量-秒（正负值）
     * @param timeFormat 返回格式
     * @return
     */
    public static String getDifferentTime(int dateOffset,int timeOffset,String timeFormat){
        String format = timeFormat != null ? timeFormat : "yyyy-MM-dd HH:mm:ss";
        GregorianCalendar calendar = (GregorianCalendar) Calendar.getInstance();
        calendar.add(10, 24 * dateOffset);
        calendar.add(12, 0);
        calendar.add(13, timeOffset);
        SimpleDateFormat formatter = new SimpleDateFormat(format);
        return formatter.format(calendar.getTime());
    }

    public static ArrayList<DayInfo> getAllDatesAndWeeks(int year, int month) {
        ArrayList<DayInfo> res = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1); // Calendar中月份从0开始
        calendar.set(Calendar.DATE, 1);
        int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        String[][] result = new String[daysInMonth][2];

        for (int i = 0; i < daysInMonth; i++) {
            DayInfo tmp = new DayInfo();
            calendar.set(Calendar.DATE, i + 1);
            String date = String.format("%02d", i + 1);
            String dayOfWeek = String.valueOf((calendar.get(Calendar.DAY_OF_WEEK)-1)==0? 7:calendar.get(Calendar.DAY_OF_WEEK)-1);
            result[i][0] = date;
            result[i][1] = dayOfWeek;
            tmp.setYear(year);
            tmp.setMonth(month);
            tmp.setDay(i + 1);
            tmp.setDetail(Integer.parseInt(dayOfWeek));
            tmp.setDisplay(1);

            LocalDate today = LocalDate.now();
            LocalDate specifiedDate = LocalDate.of(year, month, i+1);
            if (today.isAfter(specifiedDate)) {
                tmp.setIsAfter(0);
//                System.out.println("今天是指定日期之后的日期");
            } else if(today.isEqual(specifiedDate)){
                tmp.setIsAfter(0);  //设置t+1日
//                System.out.println("今天是指定日期之后的日期");
            }else{
                tmp.setIsAfter(1);
//                System.out.println("今天不是指定日期之后的日期");
            }
            res.add(tmp);
        }
        return res;
    }

    public static ArrayList<DayInfo> getAllDatesAndWeeks2(int year, int month) {
        ArrayList<DayInfo> res = new ArrayList<>();
        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.YEAR, year);
        calendar.set(Calendar.MONTH, month - 1); // Calendar中月份从0开始
        calendar.set(Calendar.DATE, 1);
        int daysInMonth = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);
        String[][] result = new String[daysInMonth][2];

        for (int i = 0; i < daysInMonth; i++) {
            DayInfo tmp = new DayInfo();
            calendar.set(Calendar.DATE, i + 1);
            String date = String.format("%02d", i + 1);
            String dayOfWeek = String.valueOf((calendar.get(Calendar.DAY_OF_WEEK)-1)==0? 7:calendar.get(Calendar.DAY_OF_WEEK)-1);
            result[i][0] = date;
            result[i][1] = dayOfWeek;
            tmp.setYear(year);
            tmp.setMonth(month);
            tmp.setDay(i + 1);
            tmp.setDetail(Integer.parseInt(dayOfWeek));
            tmp.setDisplay(1);

            LocalDate today = LocalDate.now();
            LocalDate specifiedDate = LocalDate.of(year, month, i+1);
            if (today.isAfter(specifiedDate)) {
                tmp.setIsAfter(0);
//                System.out.println("今天是指定日期之后的日期");
            } else if(today.isEqual(specifiedDate)){
                tmp.setIsAfter(1);  //设置t+1日
//                System.out.println("今天是指定日期之后的日期");
            }else{
                tmp.setIsAfter(1);
//                System.out.println("今天不是指定日期之后的日期");
            }
            res.add(tmp);
        }
        return res;
    }

    public static void main(String[] args) {
        ArrayList<DayInfo> res = getAllDatesAndWeeks(2025,1);
        for (int i = 0; i < res.size(); i++) {
            System.out.println(res.get(i));
        }
        System.out.println("=====================");
        ArrayList<DayInfo> res2 = getAllDatesAndWeeks2(2025,1);
        for (int i = 0; i < res2.size(); i++) {
            System.out.println(res2.get(i));
        }
    }
}
